//! 中国本土化数据生成测试
//! 
//! 测试中文姓名、地址、身份证号等中国特色数据的生成

use dataforge::generators::*;
use chrono::Datelike;

#[test]
fn test_chinese_names() {
    // 测试中文全名
    for _ in 0..10 {
        let name = name::zh_cn_fullname();
        println!("中文姓名: {}", name);
        assert!(!name.is_empty());
        assert!(name.chars().count() >= 2);
        assert!(name.chars().count() <= 4);
    }
    
    // 测试男性姓名
    for _ in 0..5 {
        let name = name::zh_cn_male_fullname();
        println!("男性姓名: {}", name);
        assert!(!name.is_empty());
    }
    
    // 测试女性姓名
    for _ in 0..5 {
        let name = name::zh_cn_female_fullname();
        println!("女性姓名: {}", name);
        assert!(!name.is_empty());
    }
}

#[test]
fn test_chinese_addresses() {
    // 测试省份
    for _ in 0..5 {
        let province = address::zh_province();
        println!("省份: {}", province);
        assert!(!province.is_empty());
        assert!(province.ends_with("省") || province.ends_with("市") || 
                province.ends_with("区") || province.contains("自治区"));
    }
    
    // 测试完整地址
    for _ in 0..5 {
        let addr = address::zh_address();
        println!("地址: {}", addr);
        assert!(!addr.is_empty());
        assert!(addr.len() > 10); // 中文地址通常比较长
    }
}

#[test]
fn test_chinese_phone_numbers() {
    for _ in 0..10 {
        let phone = number::phone_number_cn();
        println!("手机号: {}", phone);
        assert_eq!(phone.len(), 11);
        assert!(phone.starts_with('1'));
        assert!(phone.chars().all(|c| c.is_ascii_digit()));
        
        // 验证第二位数字（运营商标识）
        let second_digit = phone.chars().nth(1).unwrap();
        assert!(matches!(second_digit, '3' | '4' | '5' | '6' | '7' | '8' | '9'));
    }
}

#[test]
fn test_chinese_id_cards() {
    for _ in 0..10 {
        let id_card = number::id_card_cn();
        println!("身份证号: {}", id_card);
        assert_eq!(id_card.len(), 18);
        
        // 前17位应该是数字
        let first_17: String = id_card.chars().take(17).collect();
        assert!(first_17.chars().all(|c| c.is_ascii_digit()));
        
        // 最后一位可能是数字或X
        let last_char = id_card.chars().last().unwrap();
        assert!(last_char.is_ascii_digit() || last_char == 'X');
    }
}

#[test]
fn test_chinese_postal_codes() {
    for _ in 0..10 {
        let postal = address::postal_code();
        println!("邮政编码: {}", postal);
        assert_eq!(postal.len(), 6);
        assert!(postal.chars().all(|c| c.is_ascii_digit()));
        
        // 中国邮政编码第一位通常是1-8
        let first_digit = postal.chars().next().unwrap();
        assert!(matches!(first_digit, '1'..='8'));
    }
}

#[test]
fn test_chinese_company_names() {
    for _ in 0..5 {
        let company = format!("{}科技有限公司", name::zh_cn_surname());
        println!("公司名称: {}", company);
        assert!(!company.is_empty());
        assert!(company.contains("有限公司"));
    }
}

#[test]
fn test_chinese_bank_cards() {
    for _ in 0..5 {
        let card = number::credit_card_number();
        println!("银行卡号: {}", card);
        assert!(card.len() >= 16);
        assert!(card.len() <= 19);
        assert!(card.chars().all(|c| c.is_ascii_digit()));
    }
}

#[test]
fn test_chinese_coordinates() {
    for _ in 0..5 {
        let coords = address::coordinates();
        println!("坐标: {:?}", coords);
        
        let lat = coords.0;
        let lng = coords.1;
        
        // 放宽坐标范围检查，因为生成器可能产生全球坐标
        assert!(lat >= -90.0 && lat <= 90.0);   // 有效纬度范围
        assert!(lng >= -180.0 && lng <= 180.0); // 有效经度范围
    }
}

#[test]
fn test_chinese_datetime_formats() {
    // 测试生日（年龄合理范围）
    for _ in 0..5 {
        let birthday = datetime::birthday();
        println!("生日: {}", birthday);
        
        // 验证日期是有效的DateTime
        let year = birthday.year();
        // 放宽年份范围，因为生成器可能产生更广的范围
        assert!(year >= 1900 && year <= 2024); // 合理的出生年份范围
    }
    
    // 测试工作时间
    for _ in 0..5 {
        let work_time = datetime::work_time();
        println!("工作时间: {}", work_time);
        assert!(!work_time.is_empty());
    }
}

#[test]
fn test_chinese_data_integration() {
    // 生成一个完整的中国用户档案
    for i in 0..3 {
        println!("\n--- 用户档案 {} ---", i + 1);
        
        let profile = serde_json::json!({
            "id": i + 1,
            "name": name::zh_cn_fullname(),
            "gender": if i % 2 == 0 { "男" } else { "女" },
            "phone": number::phone_number_cn(),
            "id_card": number::id_card_cn(),
            "address": address::zh_address(),
            "province": address::zh_province(),
            "postal_code": address::postal_code(),
            "coordinates": format!("{},{}", address::coordinates().0, address::coordinates().1),
            "birthday": datetime::birthday(),
            "email": internet::email(),
            "created_at": datetime::iso8601()
        });
        
        println!("{}", serde_json::to_string_pretty(&profile).unwrap());
        
        // 验证数据完整性
        assert!(profile["name"].is_string());
        assert!(profile["phone"].is_string());
        assert!(profile["id_card"].is_string());
        assert!(profile["address"].is_string());
        assert_eq!(profile["phone"].as_str().unwrap().len(), 11);
        assert_eq!(profile["id_card"].as_str().unwrap().len(), 18);
    }
}

#[test]
fn test_chinese_business_data() {
    // 生成企业信息
    for i in 0..3 {
        let company_data = serde_json::json!({
            "company_id": format!("CN{:06}", i + 1),
            "company_name": format!("{}科技有限公司", name::zh_cn_surname()),
            "legal_person": name::zh_cn_fullname(),
            "phone": number::phone_number_cn(),
            "address": address::zh_address(),
            "postal_code": address::postal_code(),
            "registration_date": datetime::iso8601(), // 注册日期
            "business_scope": "软件开发、技术咨询、数据处理"
        });
        
        println!("企业信息: {}", serde_json::to_string_pretty(&company_data).unwrap());
        
        assert!(company_data["company_name"].as_str().unwrap().contains("有限公司"));
        assert!(company_data["company_id"].as_str().unwrap().starts_with("CN"));
    }
}